#include "prime_generator_proxy.h"

#include <iostream>
#include <mutex>

#include "my_service.h"

sptr<PrimeGeneratorProxy> PrimeGeneratorProxy::GetInstance()
{
    if (instance == nullptr) {
        static std::mutex mutex;
        std::lock_guard<std::mutex> lock(mutex);
        if (instance == nullptr) {
            instance = new PrimeGeneratorProxy();
        }
    }
    return instance;
}

int PrimeGeneratorProxy::GetPrime(int id)
{
    char command[20];
    snprintf(command, sizeof(command), "get %d", id);
    auto ret = MyService::GetInstance()->Call(123, command);

    int prime;
    if (sscanf(ret.c_str(), "%d", &prime) == 1) {
        return prime;
    }

    std::cout << "proxy: GetPrime failed: " << command << ": " << ret << std::endl;
    return -2;
}
